package org.auf.core;

import java.util.Date;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.Timer;

import org.apache.log4j.Logger;
import org.auf.model.dao.IAlertDAO;
import org.auf.model.dao.IPairedDAO;
import org.auf.model.dao.IPairedEventDAO;
import org.auf.model.jdbc.AlertDAOJDBC;
import org.auf.model.jdbc.PairedDAOJDBC;
import org.auf.model.jdbc.PairedEventDAOJDBC;
import org.auf.to.Alert;
import org.auf.to.Paired;
import org.auf.to.User;

public class ScoreTimer extends TimerTask {

	static Logger log = Logger.getLogger(ScoreTimer.class.getName()); 
	
	private static final long WEEK = 7*24*60*60*1000;
	private static Timer timer;
	
	public static synchronized void execute() {
		
		if (timer == null) {
			log.info("Ejecutado el ScoreTimer");
			timer = new Timer();
			timer.scheduleAtFixedRate(new ScoreTimer(), WEEK, WEEK);
		}
		
	}
	
	public static Timer getTimer() {
		return timer;
	}
	
	@Override
	public void run() {
		log.info("Iniciando la asignacion de puntos y emparejamientos");
		
		IPairedEventDAO pe = PairedEventDAOJDBC.getInstance();
		IPairedDAO pa = PairedDAOJDBC.getInstance();
		IAlertDAO alertDAO = new AlertDAOJDBC();
		UIDGenerator uid = UIDGenerator.getInstance();
		
		Iterator<User> itvol = pe.selectPairedEventVolunteers().iterator();
		Iterator<User> itgeeks = pe.selectPairedEventGeeks().iterator();
		
		Date now = new Date();
		
		while (itvol.hasNext() && itgeeks.hasNext()) {
			User volunteer = (User)itvol.next();
			User geek = (User)itgeeks.next();
			
			Paired paired = new Paired();
			paired.setId(uid.getKey());
			paired.setVolunteer(volunteer);
			paired.setGeek(geek);
			paired.setTestCompleted(false);
			paired.setDate(now);
			
			pa.insertPaired(paired);
			
			//Send alert
			Alert avolunteer = new Alert();
			Alert ageek = new Alert();
			
			avolunteer.setId(uid.getKey());
			avolunteer.setDate(now);
			avolunteer.setReceiver(geek);
			avolunteer.setTitle("Asignacion de geek");
			avolunteer.setBody("Se le ha asignado a " + geek.getName() + " como su geek de la semana.");
			avolunteer.setReaded(false);
			
			alertDAO.insertAlert(avolunteer);
			
			ageek.setId(uid.getKey());
			ageek.setDate(now);
			ageek.setReceiver(volunteer);
			ageek.setTitle("Asignacion de voluntario");
			ageek.setBody("Se le ha asignado a " + volunteer.getName() + " como su voluntario de la semana.");
			ageek.setReaded(false);
			
			alertDAO.insertAlert(ageek);
		}
		
		while (itgeeks.hasNext()) {
			User geek = (User)itgeeks.next();
			Alert ageek = new Alert();
			
			ageek.setId(uid.getKey());
			ageek.setDate(now);
			ageek.setReceiver(geek);
			ageek.setTitle("Asignacion de voluntario");
			ageek.setBody("Esta semana no hay voluntarios suficientes, gracias por su interes");
			ageek.setReaded(false);
			
			alertDAO.insertAlert(ageek);
		}
		
		while (itvol.hasNext()) {
			User volunteer = (User)itvol.next();
			Alert avolunteer = new Alert();
			
			avolunteer.setId(uid.getKey());
			avolunteer.setDate(now);
			avolunteer.setReceiver(volunteer);
			avolunteer.setTitle("Asignacion de geek");
			avolunteer.setBody("Esta semana no hay geeks suficientes, gracias por su interes");
			avolunteer.setReaded(false);
			
			alertDAO.insertAlert(avolunteer);
		}
		
		pe.deletePairedEvent();
	}

}
